﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL.Interfejsi;
using MySql.Data.MySqlClient;
using OOAD_PROJEKAT;

namespace DAL
{
    partial class DAL
    {
        public class StetaDAO : IDaoCrud<Steta>
        {

            protected MySqlCommand c;

            public long create(Steta s)
            {
                try
                {

                    c = new MySqlCommand();
                    c.CommandText = "insert into stete values (@idStete, @opisStete, @validirana, @isplata, @datumStete, @isplacena, @idAutomobila)";
                    c.Parameters.AddWithValue("@idStete", "");
                    c.Parameters.AddWithValue("@opisStete", s.OpisStete);
                    c.Parameters.AddWithValue("@validirana", s.Validirana);
                    c.Parameters.AddWithValue("@isplata", s.Isplata);
                    c.Parameters.AddWithValue("@datumStete", s.DatumStete);
                    c.Parameters.AddWithValue("@isplacena", s.Isplacena);
                    c.Parameters.AddWithValue("@idAutomobila", s.IdAutomobila);

                    c.Connection = con;
                    c.ExecuteNonQuery();

                    return c.LastInsertedId;
                }
                catch (Exception e)
                {
                    throw e;
                }
            }

            public List<Steta> getAll()
            {
                try
                {
                    c = new MySqlCommand("select * from stete", con);
                    MySqlDataReader r = c.ExecuteReader();
                    List<Steta> stete = new List<Steta>();
                    while (r.Read())
                        stete.Add(new Steta(r.GetInt32("idStete"), r.GetString("opisStete"), r.GetInt32("idAutomobila"), r.GetDouble("isplata"), r.GetDateTime("datumStete"),
                                   r.GetBoolean("validirana"), r.GetBoolean("isplacena")));

                    r.Close();
                    return stete;
                }
                catch (Exception e)
                {
                    throw e;
                }
            }


            public Steta read(Steta s)
            {
                try
                {
                    string commandStr = "select * from stete where idStete=" + s.Id;
                    c = new MySqlCommand(commandStr, con);
                    MySqlDataReader r = c.ExecuteReader();
                    Steta pr = null;
                    if (r.Read())
                        pr = new Steta(r.GetInt32("idStete"), r.GetString("opisStete"), r.GetInt32("idAutomobila"), r.GetDouble("isplata"), r.GetDateTime("datumStete"),
                                   r.GetBoolean("validirana"), r.GetBoolean("isplacena"));
                    r.Close();
                    return pr;
                }

                catch (Exception e)
                {
                    throw e;
                }
            }

            public Steta update(Steta s)
            {
                try
                {
                    String commandStr = "update stete set opisStete = ?a, ";
                    commandStr += "validirana = ?b, isplata = ?c, isplacena = ?d, ";
                    commandStr += "idAutomobila = ?e ";
                    commandStr += "where idStete=" + s.Id;

                    c = new MySqlCommand(commandStr, con);
                    c.Parameters.AddWithValue("?a", s.OpisStete);
                    c.Parameters.AddWithValue("?b", s.Validirana);
                    c.Parameters.AddWithValue("?c", s.Isplata);
                    c.Parameters.AddWithValue("?d", s.Isplacena);
                    c.Parameters.AddWithValue("?e", s.IdAutomobila);

                    c.ExecuteNonQuery();
                    return read(s);
                }
                catch (Exception e)
                {
                    throw e;
                }
            }

            public void delete(Steta s)
            {
                throw new NotImplementedException();
                /*try
                {
                    string commandStr = "delete from stete where idStete=" + s.Id;
                    
                    c = new MySqlCommand(commandStr, con);
                    c.ExecuteNonQuery();
                }
                catch (Exception e)
                {
                    throw e;
                }*/
            }

            public Steta getById(int id)
            {
                try
                {
                    Steta s = new Steta(id, "", 0, 0, DateTime.Now, false, false);
                    return read(s);
                }
                catch (Exception e)
                {
                    throw e;
                }
            }
            public List<Steta> getByExample(string name, string value)
            {
                throw new NotImplementedException();
            }
            public List<Steta> getByExample(string name, bool value)
            {
                try
                {
                    c = new MySqlCommand("select * from stete where " + name + "= " + value, con);
                    MySqlDataReader r = c.ExecuteReader();
                    List<Steta> stete = new List<Steta>();
                    while (r.Read())
                        stete.Add(new Steta(r.GetInt32("idStete"), r.GetString("opisStete"), r.GetInt32("idAutomobila"), r.GetDouble("isplata"), r.GetDateTime("datumStete"),
                                   r.GetBoolean("validirana"), r.GetBoolean("isplacena")));

                    r.Close();
                    return stete;
                }
                catch (Exception e)
                {
                    throw e;
                }
            }

        }
    }
}